<style>
    #num_rules {list-style:none;margin:0;padding:0;}
    #num_rules label{cursor:pointer;}
    #num_rules li{color:#999;padding:3px 0;background:#EFEFEF;}
    #num_rules li.active{color:#000;background:#FFc;}
</style>



    <{input type="hidden" name="id" value=$rule.id }>

    <h3 class="head-title">促销规则</h3>
    <div class="tableform" style="background:#FFF;">
        <table width="100%" border="0" cellpadding="0" cellspacing="0" >
            <tr>
                <th><em class="c-red">*</em><{t}>规则名称：<{/t}></th>
                <td><{$rule.title}></td>
            </tr>
            <tr>
                <th><em class="c-red">*</em><{t}>规则类型：<{/t}></th>
                <td>
                    <{assign var="rule_type_arr" value=array('shopLevel'=>'店铺级','goodLevel'=>'商品级')}>
                    <select id="rule_type"  name='rule_type' >
                        <option value="">请选择...</option>
                        <{foreach from=$rule_type_arr item=item key=key}>
                        <option value="<{$key}>" <{if $key==$rule.rule_type}>selected='selected'<{/if}> ><{$item}></option>
                        <{/foreach}>
                    </select>&nbsp;此项必选&nbsp;
                    <{help}><{t}>店铺级：以前端店铺为单位实施此赠品规则。商品级：以订单中的商品为单位实施此赠品规则。<{/t}><{/help}>
                </td>
            </tr>
            <tr>
                <th><em class="c-red">*</em><{t}>是否排他：<{/t}></th>
                <td>
                    <label><{if $rule.is_exclude=='1'}>是<{/if}></label>&nbsp;&nbsp;
                    <label><{if $rule.is_exclude=='2'}>否<{/if}> </label>&nbsp;&nbsp;
                    <{help}><{t}>
                    说明：1.规则是以优先级由高到低、创建时间由新到旧的顺序来逐一匹配。2.会先匹配店铺级规则，再匹配商品级规则。3.选择“是”就是排他，匹配此规则的话，就不再匹配其他的规则。选择“否”就是不排他，匹配此规则的话，就看是否有匹配的其他规则，如有并且此规则本身也是不排他的做叠加处理。
                    <{/t}><{/help}>
                </td>
            </tr>
            <tr>
                <th><em class="c-red">*</em><{t}>起止时间：<{/t}></th>
                <td>
                    <{input id="start_time" size="30" type='date' name='start_time' value=$rule.start_time vtype="required" }>
                    <{input type='select' name='start_time_hour' value=$rule.start_time_hour vtype="required" options=$conf_hours }>
                    :00:00
                    到
                    <{input id="end_time" size="30" type='date' name='end_time' value=$rule.end_time vtype="required" }>
                    <{input type='select' name='end_time_hour' value=$rule.end_time_hour vtype="required" options=$conf_hours }>
                    :00:00
                </td>
            </tr>
            <tr>
                <th><em class="c-red">*</em><{t}>时间类型：<{/t}></th>
                <td>
                    <{assign var="opt" value=array('sendtime'=>'订单处理时间','createtime'=>'订单创建时间','pay_time'=>'订单付款时间')}>
                    <{input type="select" id="time_type" name="time_type" options=$opt value=$rule.time_type}>
                </td>
            </tr>
            <tr>
                <th><em class="c-red">*</em><{t}>状　　态：<{/t}></th>
                <td>
                    <label> <{if $rule.status=='1'}>开启<{/if}></label>&nbsp;&nbsp;
                    <label><{if $rule.status=='0'}>关闭<{/if}></label>
                </td>
            </tr>
        </table>
    </div>

    <h3 class="head-title">促销条件</h3>
    <div class="tableform" style="display:;background:#FFF;">
        <table width="100%" border="0" cellpadding="0" cellspacing="0" >
            <tr>
                <th><{t}>指定店铺：<{/t}></th>
                <td>
                    <{foreach from=$shops item=items}>
                    <{if $items.checked == 'true'}>
                    <{input type="checkbox" name="shop_ids[]" value=$items.shop_id checked='checked'}>&nbsp;
                    <{$items.name}><br/>

                    <{/if}>
                    <{/foreach}>

                </td>
            </tr>
            <tr>
                <th><{t}>订单类型：<{/t}></th>
                <td>
                    <div style="width:630px;">
                        <{foreach from=$order_types item=order_type}>
                        <{if $order_type.checked == 'true'}>
                        <label class="side-width">
                            <{input type="checkbox" name="filter_arr[order_type][]" value=$order_type.type checked='checked'}>&nbsp;
                            <{$order_type.name}>
                        </label>
                        <{else}>
                        <label class="side-width">
                            <{input type="checkbox" name="filter_arr[order_type][]" value=$order_type.type}>&nbsp;
                            <{$order_type.name}>
                        </label>
                        <{/if}>
                        <{/foreach}>
                        &nbsp;&nbsp;&nbsp;<font color="#5779BD">* 不勾选表示不限制订单类型</font>
                    </div>
                </td>
            </tr>
            <tr>
                <th><{t}>指定收货区域：<{/t}></th>
                <td>
                    <div style="width:630px;">
                        <{foreach from=$provinces item=province}>
                        <{if $province.checked == 'true'}>
                        <label class="side-width">
                            <{input type="checkbox" name="filter_arr[province][]" value=$province.local_name checked='checked'}>&nbsp;
                            <{$province.local_name}>
                        </label>
                        <{else}>
                        <label class="side-width">
                            <{input type="checkbox" name="filter_arr[province][]" value=$province.local_name}>&nbsp;
                            <{$province.local_name}>
                        </label>
                        <{/if}>
                        <{/foreach}>
                        <font color="#5779BD">* 不勾选表示不限制区域</font>
                    </div>
                </td>
            </tr>
            <tr>
                <th><{t}>订单金额：<{/t}></th>
                <td>
                    <select id="order_amount_type" name="filter_arr[order_amount][type]" class="x-input">
                        <option value="0" <{if $rule.filter_arr.order_amount.type==0}>selected<{/if}>>无限制</option>
                        <option value="1" <{if $rule.filter_arr.order_amount.type==1}>selected<{/if}>>单笔付款</option>
                    </select>

                    <span style="<{if($rule.filter_arr.order_amount.type==0)}>display:none;<{/if}>">
                <select id="order_amount_sign" name="filter_arr[order_amount][sign]" class="x-input">
                    <option value="between" <{if $rule.filter_arr.order_amount.sign=='between'}>selected<{/if}>>介于</option>
                    <option value="bthan" <{if $rule.filter_arr.order_amount.sign=='bthan'}>selected<{/if}>>大于等于</option>
                </select>

                <span style="<{if($rule.filter_arr.order_amount.sign=='bthan')}>display:none;<{/if}>"><{input id="order_amount_min_num" type="text" name="filter_arr[order_amount][min_num]" value=$rule.filter_arr.order_amount.min_num size="4" }>元
                ~</span>
                <{input id="order_amount_max_num" type="text" name="filter_arr[order_amount][max_num]" value=$rule.filter_arr.order_amount.max_num size="4" }>元
            </span>
                </td>
            </tr>
            <tr>
                <th><{t}>购买商品：<{/t}></th>
                <td>
                    <select id="filter_arr[buy_goods][type]" name="filter_arr[buy_goods][type]" class="x-input">
                        <option value="0" <{if $rule.filter_arr.buy_goods.type=='0'}>selected<{/if}>>任意商品</option>
                        <option value="1" <{if $rule.filter_arr.buy_goods.type=='1'}>selected<{/if}>>指定商品</option>
                    </select>

                    <div id="bookgoodsbn" style="<{if($rule.filter_arr.buy_goods.type==0)}>display:none;<{/if}>padding:10px 0 0 0;line-height:28px;">

                        购买范围：
                        <select id="filter_arr[buy_goods][buy_type]" name="filter_arr[buy_goods][buy_type]" class="x-input">
                            <option value="any" <{if $rule.filter_arr.buy_goods.buy_type=='any'}>selected<{/if}>>购买了任意一个指定商品</option>
                            <option value="all" <{if $rule.filter_arr.buy_goods.buy_type=='all'}>selected<{/if}>>购买了全部指定商品</option>
                            <option value="none" <{if $rule.filter_arr.buy_goods.buy_type=='none'}>selected<{/if}>>排除购买的指定商品</option>
                        </select>
                        <{help}><{t}>当设定为购买了全部指定商品时，购买数量以最少的单品数量为准。<{/t}><{/help}>
                        <br/>

                        <div id="ajax_get_div_contents"></div>

                        <div id="anchors" style="padding: 10px 0 0 0; line-height: 28px; width: 625px;margin-top:-8px;"></div>
                    </div>
                </td>
            </tr>
            <tr>
                <th><{t}>赠送数量：<{/t}></th>
                <td>
                    <ul id="num_rules">
                        <li>
                            <input name="filter_arr[buy_goods][num_rule]" type="radio" value="fixed" <{if $rule.filter_arr.buy_goods.num_rule=='fixed'}>checked<{/if}> />

                            购买
                            <select id="num_rules_sign" name="filter_arr[buy_goods][rules_sign]" class="x-input">
                                <option value="nequal" <{if $rule.filter_arr.buy_goods.rules_sign=='nequal'}>selected<{/if}>>等于</option>
                                <option value="between" <{if $rule.filter_arr.buy_goods.rules_sign=='between'}>selected<{/if}>>介于</option>
                                <option value="bthan" <{if $rule.filter_arr.buy_goods.rules_sign=='bthan'}>selected<{/if}>>大于等于</option>
                            </select>
                            <{input type="text" id="buy_goods_min_num" name="filter_arr[buy_goods][min_num]" value=$rule.filter_arr.buy_goods.min_num size="4" }>

                            <span style="<{if($rule.filter_arr.buy_goods.rules_sign!='between')}>display:none;<{/if}>">
                        ~ <{input id="buy_goods_max_num" type="text" name="filter_arr[buy_goods][max_num]" value=$rule.filter_arr.buy_goods.max_num size="4" }>
                        </span>

                            <select id="count_type" name="filter_arr[buy_goods][count_type]" class="x-input">
                                <option value="num" <{if $rule.filter_arr.buy_goods.count_type=='num'}>selected <{/if}> >件</option>
                                <option value="paid" <{if $rule.filter_arr.buy_goods.count_type=='paid'}>selected<{/if}>>元</option>
                                <option value="sku" <{if $rule.filter_arr.buy_goods.count_type=='sku'}>selected<{/if}>>种</option>
                            </select>
                            时送一组赠品，不累加
                            <{help}><{t}>金额是指订单的实际付款金额。种数是指不重复的基础物料编码数量结合购买商品项选择指定商品填写的基础物料编码。 <{/t}><{/help}>
                        </li>
                        <li>
                            <input id="num_rules_auto" name="filter_arr[buy_goods][num_rule]" type="radio" value="auto" <{if $rule.filter_arr.buy_goods.num_rule=='auto'}>checked<{/if}> />
                            每购买
                            <{input type="text" id="per_num" name="filter_arr[buy_goods][per_num]" value=$rule.filter_arr.buy_goods.per_num size="4" }>
                            <select id="count_type2" name="filter_arr[buy_goods][count_type2]" class="x-input">
                                <option value="num" <{if $rule.filter_arr.buy_goods.count_type2=='num'}>selected<{/if}>>件</option>
                                <option value="paid" <{if $rule.filter_arr.buy_goods.count_type2=='paid'}>selected<{/if}>>元</option>
                                <option value="sku" <{if $rule.filter_arr.buy_goods.count_type2=='sku'}>selected<{/if}>>种</option>
                            </select>
                            ，

                            赠送<{input type="text" id="send_suite" name="filter_arr[buy_goods][send_suite]" value=$rule.filter_arr.buy_goods.send_suite size="4" }>组赠品，

                            每订单最多送<{input type="text" id="max_send_suite" name="filter_arr[buy_goods][max_send_suite]" value=$rule.filter_arr.buy_goods.max_send_suite size="4" }>组

                            <{help}><{t}>系统自动累加赠送 如：买2送1，4送2、6送3。1组赠品可以包含多个商品和多个数量。例如买2送1组，最多送5组， 当1组赠品包含2个A商品和1个B商品，将送2个A和1个B。买4时送4个A和2个B，依次累加。最多送10个A和5个B。计算倍数时无条件舍去小数位。<{/t}><{/help}>
                        </li>
                    </ul>
                </td>
            </tr>
            <tr>
                <th><{t}>限量赠送：<{/t}></th>
                <td>
                    <select id="filter_arr[buy_goods][limit_type]" name="filter_arr[buy_goods][limit_type]" class="x-input">
                        <option value="0" <{if $rule.filter_arr.buy_goods.limit_type=='0'}>selected<{/if}>>不限量</option>
                        <option value="1" <{if $rule.filter_arr.buy_goods.limit_type=='1'}>selected<{/if}>>限量</option>
                    </select>

                    <span style="<{if($rule.filter_arr.buy_goods.limit_type==0)}>display:none;<{/if}>">
                符合条件的前
                <{input type="text" id="buy_goods_limit_orders" name="filter_arr[buy_goods][limit_orders]" value=$rule.filter_arr.buy_goods.limit_orders size="4" }>笔订单赠送
             </span>
                </td>
            </tr>
        </table>
    </div>

    <style>
        .side-width{
            margin-left: 30px;
        }
    </style>

    <h3 class="head-title">赠送商品</h3>
    <div class="tableform" style="display:;background:#FFF;">
        <table width="100%" border="0" cellpadding="0" cellspacing="0" >

            <tr>
                <td>
                    <table id="gift_list" class="gridlist">
                        <thead>
                        <tr>

                            <th>序号</th>
                            <th>商家编码</th>
                            <th>商品名称</th>
                            <th>成本价</th>
                            <th>赠送数量</th>
                        </tr>
                        </thead>
                        <tbody>

                        <{foreach from=$gifts item=data key=k}>
                        <tr style="display:;background:#FFFFCC;">

                            <td><{$k+1}></td>
                            <td><{$data.gift_bn}></td>
                            <td><{$data.gift_name}></td>
                            <td><{$data.gift_price}></td>
                            <td>
                                <{$data.num}>
                            </td>
                        </tr>
                        <{/foreach}>
                        </tbody>
                    </table>
                </td>
            </tr>
        </table>
    </div>



<div id="form_preview" style="display:none;padding:5px;">
    <h3 align="center">规则预览</h3>
    <div  class="division" style="padding:10px;line-height:2em;">
        <table>
            <col width="20%" align="right" />
            <col width="80%" align="left" />
            <tr>
                <th>活动名称：</th><td><b id="pre_title"><!--2014年父亲节赠送贺卡--></b></td>
            </tr>
            <tr>
                <th>有 效 期：</th><td>
                <b id="pre_time_type"></b> -
                <b id="pre_start_time"><!--2014-06-21--></b>  至  <b id="pre_end_time"><!--2014-06-28--></b>
            </td>
            </tr>
            <tr>
                <th>赠送规则：</th><td>
                <div id="pre_rules" style="border-radius:6px;padding:5px;background:#FFF7B2;">

                </div>
            </td>
            </tr>
            <tr>
                <th>赠送商品：</th><td><div id="pre_gifts" style="border-radius:6px;margin:2px 0 0 0;padding:5px;background:#DBE2ED;"><!--6023Y0098|上海商派ShopEx KPI 企业版 使用权1年--></div></td>
            </tr>
        </table>
    </div>
</div>



<script>

    var count_type = '<{$rule.filter_arr.buy_goods.count_type}>';
    var gift_template = '';//赠品列表tr模板

    // 赠品数量控制
    function chk_num(obj){
        if(obj.value <= 0 || obj.value == ''){
            alert('赠品数必须大于0');
            obj.value = 1;
            return false;
        }
    }







    //设置背景色
    function set_bg_color(e){
        if(e.checked === true){
            e.getParent().getParent().style.backgroundColor = '#FFFFCC';
        }else{
            e.getParent().getParent().style.backgroundColor = '';
        }
    }





    function product_selected_show(){
        new Dialog('index.php?app=ome&ctl=admin_crm_gift&act=showProducts',{
            ajaxoptions:{data:$('hand-selected-product').getNext('div'),method:'post'}
        });
    }

    void function (){
        var apply_id	= '<{$rule.id}>';
        $ES('input[name="shop_ids[]"]').addEvent('click',function()
        {
            //var apply_id	= '';
            ajax_onload_html(apply_id);
        });

        ajax_onload_html(apply_id);
    }();

    function ajax_onload_html(apply_id)
    {
        var shop_ids	= '';
        $$('input[name="shop_ids[]"').each(function(item){
            if(item.checked==true && item.get('value'))
            {
                shop_ids += ',' + item.get('value');
            }
        })

        if(shop_ids.indexOf("_ALL_") > 0 )
        {
            shop_ids	= '_ALL_';
        }
        shop_ids    = shop_ids.substr(1);

        new Request.HTML({
            url:'index.php?app=ome&ctl=admin_crm_gift&act=ajax_sales_material_html&p[0]='+ apply_id +'&p[1]='+shop_ids,
            method:'post',
            update:$('ajax_get_div_contents'),
            data:{'apiName':'request_html'},
            onSuccess:function(){

            }
        }).send();
    }

</script>
